package com.xhdx.threadpool;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class InVokeAnyTest {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();  
        Set<Callable<String>> callables = new HashSet<Callable<String>>();  
          
        callables.add(new Callable<String>() {  
            public String call() throws Exception {  
                System.out.println("任务 1");
                return "任务 1";  
            }  
        });  
        callables.add(new Callable<String>() {  
            public String call() throws Exception {
                System.out.println("任务 2");
                return "任务 2";  
            }  
        });  
        callables.add(new Callable<String>() {  
            public String call() throws Exception {
                System.out.println("任务 3");
                return "任务 3";  
            }  
        });  
          
        String result = null;
        try {
            result = executorService.invokeAny(callables);
        } catch (Exception e) {
            e.printStackTrace();
        } 
        System.out.println("结果  = " + result);  
        executorService.shutdown();
    }
}
